var app =getApp()
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentid:0,
        cardCur: 0,
        swiperList: [],
        limit:5,
        writelist:'',
        searchvalue: '',
        isDuaration:true,
        isNone:false,
        categoryList:[
            { id: 1, category: '楼盘动态', imgurl: '../../static/images/icon/icon1.png'},
            { id: 2, category: '新闻资讯', imgurl: '../../static/images/icon/icon2.png'},
            { id: 3, category: '我的原创', imgurl: '../../static/images/icon/icon3.png'},
            { id: 4, category: '我的日记', imgurl: '../../static/images/icon/icon4.png'},
            { id: 5, category: '热点事件', imgurl: '../../static/images/icon/icon5.png'},
            { id: 6, category: '小道消息', imgurl: '../../static/images/icon/icon6.png'},
        ]
    },

    searchlist(e) {
        this.setData({
            searchvalue: e.detail.value
        })
    },
    searchdesc() {
        var that = this;
        var value = that.data.searchvalue;
        wx.showLoading({
            title: '搜索中',
        })
        if (value == '') {
            wx.showToast({
                title: '请输入搜索内容',
                duration: 1000,
                icon: 'none'
            })
        } else {
            var setid = app.globalData.cityCode;

            app.core('get', 'xiaochengxu_article_list/limit/5', {
                keywords: value
            }, (res => {
                console.log(res)
                if (res.data.length == 0) {
                    wx.showToast({
                        title: '暂无相关信息',
                        duration: 1000,
                        icon: 'none'
                    })
                } else {
                    wx.navigateTo({
                        url: './writesearch?keyword=' + value + '',
                    })
                }
                wx.hideLoading()
            }))
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // let isLogin = wx.getStorageSync('isLogin');
        // if (isLogin == 1) {
        //   console.log('用户id存在')
        // } else {
        //   wx.showModal({
        //     title: '提示',
        //     content: '您还没有登录，无法使用写一写功能，请登录后体验更多功能',
        //     success(res) {
        //       if (res.confirm) {
        //         wx.switchTab({
        //           url: '/pages/member/index',
        //         })
        //       } else if (res.cancel) {
        //         wx.switchTab({
        //           url: '/pages/member/index',
        //         })
        //       }
        //     }
        //   })
        // }
        let _this = this;
        // _this.guanggao();
        // _this.list(5)
        setTimeout(() => {
            _this.setData({
                isDuaration:false
            })
        },1000)
    },
    guanggao() {
        var that = this;
        var setid = app.globalData.cityCode;
        app.core('get', 'get_banner_advert/site_id/' + setid +'', '', (res) => {
            console.log(res)
            that.setData({
                swiperList: res.data
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    list(limit){
        var that = this;
        var category = this.data.currentid;
        var num = limit +5;
        // var isNone = that.data.isNone;
        // that.setData({
        //   isNone: false
        // })
        // wx.showLoading({
        // 	title: '加载中..',
        // })
        app.core('get', 'xiaochengxu_article_list/limit/' + limit + '?category=' + category,'',(res)=>{
            console.log(res,999)
            var arr = res.data;
            if(arr.length === 0){
                // console.log('111')
                that.setData({
                    isNone:true
                })
            }else{
                that.setData({
                    isNone:false
                })
            }

            that.setData({
                limit: num,
                writelist:arr,
                // writelist: that.data.writelist.concat(res.data)
            })
            // setTimeout(()=>{
            //   wx.hideLoading()
            // },200)
            // wx.hideLoading()
        })
    }, // 写一写列表

    getLocalTime(date) {
        // return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
        var date = new Date(date * 1000);//如果date为13位不需要乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
        var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
        return Y + M + D + h + m + s;


    }, // 转换时间戳
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // console.log(1111)
        // let isLogin = wx.getStorageSync('isLogin');
        // if (isLogin == 1) {
        //   console.log('用户id存在')
        // } else {
        //   wx.showModal({
        //     title: '提示',
        //     content: '您还没有登录，无法使用写一写功能，请登录后体验更多功能',
        //     success(res) {
        //       if (res.confirm) {
        //         wx.switchTab({
        //             url: '/pages/member/index',
        //           })
        //       } else if (res.cancel) {
        //         wx.switchTab({
        //           url: '/pages/member/index',
        //         })
        //       }
        //     }
        //   })
        // }
        var key = wx.getStorageSync('citykey');
        console.log(key)
        if (key == true) {
            this.list(5)
            this.guanggao()
        }
    },

    /**
     *切换分类
     */
    bindCate(e){
        var cateid = e.currentTarget.dataset.id;
        var currentid = this.data.currentid;
        console.log(currentid,666666)

        if (cateid != currentid){
            this.setData({ currentid: cateid })
        }else{
            this.setData({ currentid: 0 })
        }

        this.list(5)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showLoading({
            title: '努力刷新...',
        })
        setTimeout(() => {
            wx.hideLoading()
        },1000)
        this.list(5)
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var limit = this.data.limit
        this.list(limit)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    advJump (e) {

        let swiperList = this.data.swiperList

        let val = swiperList[e.currentTarget.dataset.index]
        if (val) {
            if ( val['app_id'] != undefined && val['app_id'] != null && val['app_id'] != '') {
                wx.navigateToMiniProgram({
                    appId: val['app_id'],
                    path: val['redirect_url'],
                    extraData: {},
                    envVersion: 'release',
                    success(res) {
                        console.log(res)
                        // 打开成功
                    },
                    fail(e) {
                        console.log(e)
                    }
                })
            } else if ( val['redirect_url'] != undefined && val['redirect_url'] != null && val['redirect_url'] != '') {

                let url = val['redirect_url']

                if (url.indexOf("/pages") == 0) {
                    wx.navigateTo({
                        url: url,
                    })
                } else if (url.indexOf("http") == 0) {
                    wx.navigateTo({
                        url: '/pages/webView/index?url='+ decodeURIComponent(url),
                    })
                }
            }

        }
    }
})
